﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using QLDV.Models;
using QLDV.ViewModels;
namespace QLDV.Controllers
{
    public class LoaiCSDController : BaseController
    {
        //
        // GET: /LoaiCSD/
        

        public ActionResult Index()
        {
            return View(Repository.GetAllLoaiCSD());
        }

        //
        // GET: /LoaiCSD/Create
        [HttpGet]
        public ActionResult Create()
        {
            LoaiCSDModel loaiCSD = new LoaiCSDModel();
            List<LOAICSD> capQL = Repository.GetAllLoaiCSD();
            List<SelectListItem> resultList = new List<SelectListItem>();
            foreach(var item in capQL)
            {
                resultList.Add(new SelectListItem
                    {   
                        Value = item.Id_LoaiCSD.ToString(),
                        Text = item.Ten
                    }
                );
            }
            ViewData["capQL"] = resultList;
            //if (Request != null && Request.IsAjaxRequest())
            //{
                return PartialView(loaiCSD);
            //}
            //else return RedirectToAction("Index");
        } 

        //
        // POST: /LoaiCSD/Create

        [HttpPost]
        public ActionResult Create(LoaiCSDModel loaiCsd)
        {
            if(loaiCsd.Id_LoaiCSDQL != null)
            {
                loaiCsd.Order = Repository.GetByIdLoaiCSD((int)loaiCsd.Id_LoaiCSDQL).CapBac + 1;
            }
            else
            {
                loaiCsd.Order = 0;
            }
            string message = Repository.InsertLoaiCSD(loaiCsd);
            if (message.Equals("true"))
            {
                return RedirectToAction("Index");
            }
            else return RedirectToAction("Index","Error", message);
            
        }
        
        //
        // GET: /LoaiCSD/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Request != null && Request.IsAjaxRequest())
            {
                var result = Repository.GetByIdLoaiCSD(id);
                LoaiCSDModel loaicsd = new LoaiCSDModel(result);
                if (result.Id_LoaiCSDQL != null)
                {
                    ViewData["TenLoaiCSDQL"] = Repository.GetByIdLoaiCSD((int)result.Id_LoaiCSDQL).Ten;
                }
                else ViewData["TenLoaiCSDQL"] = "";
                return PartialView(loaicsd);
            }
            else return RedirectToAction("Index");
                
        }

        //
        // POST: /LoaiCSD/Edit/5

        [HttpPost]
        public ActionResult Edit(LOAICSD loaiCSD)
        {
            try
            {
                Repository.UpdateLoaiCSD(new LoaiCSDModel { 
                   Id = loaiCSD.Id_LoaiCSD,
                   Name = loaiCSD.Ten,
                   Desc = loaiCSD.MoTa,
                   DoanPhi = (bool)loaiCSD.DoanPhi
                });
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

       
    }
}
